<?php
/*
 * 
 */
class mod_menu extends module {


	public function init(){
	}

	public function itemslist(){
		$language=(isset($_GET["lang"]) && in_array($_GET["lang"],$this->api->syslanguages())?$_GET["lang"]:$this->api->language());
		$level = (isset($_GET["level"]) && is_numeric($_GET["level"]))?$_GET["level"]:$this->api->get("accesslevel");
		if($this->api->checkPOST("level","lang","change") ){
			list($chlevel,$chlang)=$this->api->escapePOST("level","lang");
			$this->api->base->sendHeader("menu-itemslist","level=".$chlevel."&lang=".$chlang);	
		}
		if($this->api->checkPOST("name","parent","weight","function","Submit")){
		list($name,$parent,$weight,$function) = $this->api->escapePOST("name","parent","weight","function");
		if(preg_match("/^([A-z0-9_]{1,20})-([A-z0-9-_]{1,240})$/",$function,$term)){
			$this->api->dbo->insert("langterms",array("term"=>$this->api->base->GetHash($term[1],$term[2]),"value"=>$name,"lang"=>$language,"alias"=>$function,"module"=>$term[1],"method"=>$term[2],"image"=>"","url"=>"","param"=>"","parent"=>$parent,"weight"=>$weight) );	
		} else { }
		}
		$Levels = $this->api->dbo->select("levels","id,name");
		$tree=$this->gettree(0,0,$language,$level);
		$Functions=$this->api->dbo->select("functions","alias","where hash in (select hash from levellinks where level='".$level."' ) ");
		return $this->api->view->template("itemslist",array("levels"=>&$Levels,"cl"=>$level,"clang"=>$language,"languages"=>$this->api->syslanguages(),"functions"=>&$Functions,"tree"=>&$tree,"seltree"=>$this->gettree(0,0,$language,$level,true) ) );
	}

	private function gettree($parent=0,$depth=0,$lang,$level,$select=false,$current=0){
		$Query = $this->api->dbo->select("langterms","id,value","where lang='".$lang."' and parent='".$parent."' and term in ( select hash from levellinks where level='".$level."')");
		global $depth;
		$depth++;
		$t=null;
		if(count($Query)>0){
			$t.=(!$select)?"<ul>":null;
			$i=0;
		while($i<count($Query)){
			$o=null;for($j=1;$j<$depth;$j++)$o.="-&nbsp;";
        		$t.=($select==false)?'<li>'.$Query[$i]["value"].'&nbsp;-&nbsp;&nbsp;&nbsp;<a href="'.$this->api->base->GetActionLink("menu-edititem","item=".$Query[$i]["id"]."&lang=".$lang."&level=".$level).'" >[E]</a>&nbsp;&nbsp;<a href="'.$this->api->base->GetActionLink("menu-deleteitem","item=".$Query[$i]["id"]."&lang=".$lang."&level=".$level).'" >[X]</a></li>':'<option value="'.$Query[$i]["id"].'" '
                        .($current==$Query[$i]["id"]?"selected":null).' >'.$o.$Query[$i]["value"].'</option>';
        		$t.=$this->gettree($Query[$i]["id"],$depth,$lang,$level,$select,$current);
       			$i++;
        		$depth--;
        	}
        $t.=(!$select)?"</ul>":null;
		}
		return $t;
	}

	public function edititem(){
		$language=(isset($_GET["lang"]) && in_array($_GET["lang"],$this->api->syslanguages()))?$_GET["lang"]:$this->api->language();
		$level = (isset($_GET["level"]) && is_numeric($_GET["level"]))?$_GET["level"]:$this->api->accesslevel();
		if(!isset($_GET["item"]) || !is_numeric($_GET["item"])){
    		return null;
		}
		$item = $this->api->escapeString($_GET["item"]);
		if($this->api->checkPOST("name","lang","parent","weight") ){
			list($name,$lang,$parent,$weight) = $this->api->escapePOST("name","lang","parent","weight");
			$name=trim($name);
			if(strlen($name)<=0 || !in_array($lang,$this->api->syslanguages()) || !is_numeric($parent) || !is_numeric($weight)  ){
			} else {
				$this->api->dbo->update("langterms","set value='$name',lang='$lang',parent='$parent',weight='$weight' where id='$item' ");
			}
		}
		$this->api->base->addWay("itemslist",null,$this->lang->get("m1") );
		$Query = $this->api->dbo->select("langterms","value,alias,lang,parent,weight","where id='$item' limit 1 offset 0",_RESULT_ROW);
		if(count($Query)==0)
			return null;
		return $this->api->view->template("edit",array("data"=>&$Query,"lang"=>
    	$this->api->syslanguages(),"parent"=>$this->gettree(0, 0, $language, $level, true,$Query["parent"])));
	}

	public function deleteitem(){
		if(isset($_GET["item"]) && is_numeric($_GET["item"]) ){
			$this->api->dbo->delete("langterms"," where id='".$_GET["item"]."' ");
		}
		$language=(isset($_GET["lang"]) && in_array($_GET["lang"],$this->api->syslanguages()))?$_GET["lang"]:$this->api->language();
		$level = (isset($_GET["level"]) && is_numeric($_GET["level"]))?$_GET["level"]:$this->api->accesslevel();
		$this->api->redirect("menu-itemslist","level=".$level."&lang=".$language);
	}

	public function install(){
	}

	public function uninstall(){
	}
}
?>